package com.xiao.xin.core.service.Impl;

import com.xiao.xin.core.service.OllamaChatService;
import com.xiao.xin.ollama.client.api.OllamaApi;
import com.xiao.xin.ollama.client.domain.OllamaMessage;
import com.xiao.xin.ollama.client.dto.OllamaChatDto;
import com.xiao.xin.ollama.client.vo.OllamaChatVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class OllamaChatServiceImpl implements OllamaChatService {

    private final OllamaApi ollamaApi;

    @Override
    public String translate(String prompt) {
        OllamaMessage message = new OllamaMessage();
        message.setRole("user");
        message.setContent("请把下面文本翻译成英文：" + prompt);
        try {
            OllamaChatVo ollamaChatVo = ollamaApi.chat(OllamaChatDto
                    .builder()
                    .model("qwen2:0.5b")
                    .messages(List.of(message))
                    .build()).execute().body();
            return ollamaChatVo.getMessage().getContent();
        } catch (IOException e) {
            log.info(e.getMessage());
        }
        return prompt;
    }
}
